package com.farm.system.controller;

import com.farm.constant.MessageConstant;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.system.pojo.Menu;
import com.farm.system.service.MenuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 菜单控制层
 *
 * @author Chengfei
 * @date 2020/10/11
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有菜单 用于回显所有菜单供用户选择
     *
     * @return 菜单集合
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        return new Result(true, StatusCode.OK, MessageConstant.SELECT_ALL_MENU_SUCCESS, menuList);
    }

    /**
     * 条件分页查询
     *
     * @param page      页码
     * @param size      大小
     * @param searchMap 条件
     * @return 菜单集合
     */
    @GetMapping("/findPageByCondition")
    public Result findPageByCondition(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                      @RequestParam(name = "size", required = false, defaultValue = "10") Integer size,
                                      @RequestParam Map<String, Object> searchMap) {

        Page<Menu> pageList = menuService.findPageByCondition(page, size, searchMap);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, MessageConstant.SELECT_MENU_SUCCESS, pageResult);
    }


    /**
     * 添加菜单
     *
     * @param menu 菜单
     * @return 无
     */
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu) {
        menuService.add(menu);
        return new Result(true, StatusCode.OK, MessageConstant.ADD_MENU_SUCCESS);
    }

    /**
     * 更新菜单
     *
     * @param menu 菜单
     * @return 无
     */
    @PutMapping("/update")
    public Result update(@RequestBody Menu menu) {
        menuService.update(menu);
        return new Result(true, StatusCode.OK, MessageConstant.UPDATE_MENU_SUCCESS);
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单id
     * @return 无
     */
    @DeleteMapping("/delete/{menuId}")
    public Result delete(@PathVariable("menuId") Integer menuId) {
        menuService.delete(menuId);
        return new Result(true, StatusCode.OK, MessageConstant.DELETE_MENU_SUCCESS);
    }


}
